package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.BehaviorDto;
import com.heima.behavior.ReadBehaviorDto;
import com.heima.behavior.context.BaseContext;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    private CacheService cacheService;

    /**
     * 用户对文章的点赞
     *
     * @param behaviorDto 前端dto
     */
    @Override
    public void likeBehavior(BehaviorDto behaviorDto) {
        //参数校验
        if (behaviorDto == null || behaviorDto.getArticleId() == null) {
            return;
        }

        //游客登录 userid=0
        //用户id
        Long userId = BaseContext.getCurrentId();
        if (userId == 0) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("behaviorDto", behaviorDto);
        hashMap.put("userId", userId);
        kafkaTemplate.send(ArticleConstants.ARTICLE_LIKE_BEHAVIOR, JSON.toJSONString(hashMap));
    }

    /**
     * 文章阅读数
     * 无论是否是登录的用户,都统计浏览量
     * @param readBehaviorDto 前端dto
     */
    @Override
    public void readBehavior(ReadBehaviorDto readBehaviorDto) {
        //参数校验
        if (readBehaviorDto == null ||
                readBehaviorDto.getArticleId() == null ||
                readBehaviorDto.getCount() == null) {
            return;
        }
        if (BaseContext.getCurrentId() == null) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("readBehaviorDto",readBehaviorDto);
        data.put("userId",BaseContext.getCurrentId());
        kafkaTemplate.send(ArticleConstants.ARTICLE_READ_BEHAVIOR, JSON.toJSONString(data));
    }

    /**
     * 不喜欢某一篇文章
     *
     * @param behaviorDto 前端dto
     */
    @Override
    public void unLikesBehavior(BehaviorDto behaviorDto) {
        //用户id
        Long userId = BaseContext.getCurrentId();
        //参数校验
        if (behaviorDto == null ||
                behaviorDto.getArticleId() == null ||
                behaviorDto.getType() == null) {
            return;
        }
        if (userId == 0) {
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("behaviorDto", behaviorDto);
        hashMap.put("userId", userId);
        kafkaTemplate.send(ArticleConstants.UN_LIKE_BEHAVIOR, JSON.toJSONString(hashMap));
    }
}
